package com.hitgo.soccer.core;

import java.io.Serializable;

import com.hitgo.soccer.config.MatchConfig;
import com.hitgo.soccer.config.PlayerConfig;
import com.hitgo.soccer.config.PositionConfig;

public class Player  implements Serializable
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= -8581846195695339911L;
	public static final int TYPE_GOALKEEPER = 0;		// 守门员
	public static final int TYPE_REARGUARD = 1;			// 后卫
	public static final int TYPE_MIDFIELDER = 2;		// 中场
	public static final int TYPE_VANGUARD = 3;			// 前锋

	private long 	pauseTime = 0;
	private Short 	position;
	private Short	level;						//	球员等级
	
	private PositionConfig 	posData;
	private PlayerConfig	playerData;
	
	
	// 球员信息
	private Integer playerID;
	private short x;
	private short y;
	
	private Long userID;
	
	// 守门员的属性  reaction == 球员的 speed
	public int getReaction()
	{
		return this.playerData.speed;
	}

	// 位置感 pointSense 对应  球员的 vision 
	public int getPointSense()
	{
		return playerData.vision;
	}

	//	catched;	 手控 == 球员的射门
	public int getCatched()
	{
		return playerData.shot;
	}

	public Player(short playerID)
	{
		PlayerConfig pc = PlayerConfig.getPlayerConfig(playerID);
		this.setPlayerID(playerID);
		this.setPlayerData(pc);
	}
	
	public Player()
	{
		
	}

	public int getPlayerID()
	{
		return playerID;
	}
	
	public void setPlayerID(int id)
	{
		this.playerID = id;
	}

	public short getX()
	{
		return x;
	}
	
	public void setX(short positionX)
	{
		this.x = positionX;
	}

	public short getY()
	{
		return y;
	}
	
	public void setY(short positionY)
	{
		this.y = positionY;
	}
	
	public void pause()
	{
		this.pauseTime = System.currentTimeMillis();
	}
	
	public boolean isPause()
	{
		if(this.pauseTime > 0)
			return this.pauseTime + MatchConfig.playerPauseInterval < System.currentTimeMillis();
		return false; 
	}
	
	// 判断防守范围
	public boolean inDRange(int x, int y)
	{
		// 判断防守范围
		if(Math.abs(this.getX() - x) < 10)
			return true;
		return false;
	}

	public int getShot()
	{
		return playerData.shot;
	}

	public int getStrength()
	{
		return playerData.strength;
	}

	public short getInitX()
	{
		return this.posData.initX;
	}


	public short getInitY()
	{
		return this.posData.initY;
	}

	public int getVision()
	{
		return playerData.vision;
	}

	public int getPass()
	{
		return playerData.pass;
	}

	public int getConscious()
	{
		return playerData.conscious;
	}

	public int getSpeed()
	{
		return this.playerData.speed;
	}

	public int getDribble()
	{
		return playerData.dribble;
	}

	public int getEnergy()
	{
		return playerData.energy;
	}

	public int getBalance()
	{
		return playerData.balance;
	}

	public int getFierce()
	{
		return playerData.fierce;
	}

	public int getObstruct()
	{
		return playerData.obstruct;
	}

	public int getSteal()
	{
		return playerData.steal;
	}
	
	public Player clone()
	{
		Player newP = new Player();
		
		
		newP.setPlayerID(this.getPlayerID());
		
		
		newP.setPosData(getPosData());
		newP.setPlayerData(getPlayerData());
		return newP;
	}
	
	public PlayerConfig getPlayerData()
	{
		return this.playerData;
	}

	public void setPlayerData(PlayerConfig plConfig)
	{
		this.playerData = plConfig;
	}
	
	public short getPosition()
	{
		return position;
	}

	public void setPosition(short posistion)
	{
		this.position = posistion;
	}

	public PositionConfig getPosData()
	{
		return posData;
	}

	public void setPosData(PositionConfig posData)
	{
		this.posData = posData;
		this.x = posData.initX;
		this.y = posData.initY;
	}

	public long getUserID()
	{
		return userID;
	}

	public void setUserID(long userID)
	{
		this.userID = userID;
	}
	
	public boolean isRange(short X, short Y)
	{
		if(X >= this.posData.rangTopX && X >= this.posData.rangBottomX
				&& Y >= this.posData.rangTopY && Y >= this.posData.rangBottomY)
			return true;
			
		return false;
	}

	public boolean isRange()
	{
		if(this.x >= this.posData.rangTopX && this.x >= this.posData.rangBottomX
				&& this.y >= this.posData.rangTopY && this.y >= this.posData.rangBottomY)
			return true;
			
		return false;
	}
	
	public void move(boolean isL2R)
	{
		short nSpeed = getMoveSpeed();
		
		if(isL2R == false)
			nSpeed = (short) -nSpeed;
		this.x += nSpeed;
		
		if(x < this.posData.rangTopX)
			x = this.posData.rangTopX;
		if(x > this.posData.rangBottomX)
			x = this.posData.rangBottomX;
		
		if(this.y < this.posData.rangTopY)
			this.y = this.posData.rangTopY;
		if(this.y > this.posData.rangBottomY)
			this.y = this.posData.rangBottomY;
	}
	
	public short getMoveSpeed()
	{
		short nSpeed = (short)((getSpeed()*0.25+15) * 45 / MatchConfig.maxRound);
		
		return nSpeed;
	}
}
